package com.llkj.qingnianguoxue;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.llkj.qingnianguoxue.home.MasterSubscribeActivity;
import com.llkj.qingnianguoxue.mine.BalanceActivity;
import com.llkj.qingnianguoxue.zhuanlan.ZhiBoDetailActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * <p/>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

            notificationType(context, bundle, 1);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");

            //打开自定义的Activity
            notificationType(context, bundle, 2);
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    private void notificationType(Context context, Bundle bundle, int type) {
        if (bundle.containsKey(JPushInterface.EXTRA_EXTRA)) {
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            JSONObject extraObject;
            try {
                extraObject = new JSONObject(extra);//type,id,txt
                if (extraObject.has("type")) {// 自定义的处理
                    if(type==2){//点击通知
                        type = Integer.parseInt(extraObject.getString("type"));
                        entryDetails(context,type,extraObject.getString("txt"));
                    }else if(type==1){//收到通知
                        Intent intent = new Intent();  //通知有新消息,1系统通知，2聊天
                        intent.putExtra("msg_data", "1");
                        intent.setAction(MainActivity.NOTIFICATION_MESSAGE);
                        context.sendBroadcast(intent);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

    /**
     * 进入详情
     * 1添加好友申请
     * 2订阅大师到期
     * 3直播开始通知
     * 4同意好友申请
     * 5不同意好友申请
     * 6删除好友
     * 7订阅app即将到期
     */
    public static void entryDetails(Context context, int type, String id){
        Intent intent = null;
        Bundle bundle = new Bundle();
        switch (type){
            case 1://1添加好友申请
                break;
            case 2://2订阅大师到期
                intent = new Intent(context,MasterSubscribeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                bundle.putString("id", id);
                intent.putExtras(bundle);
                context.startActivity(intent);
                break;
            case 3://3直播开始通知
//                intent = new Intent(context,ZhiBoDetailActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                bundle.putString("id", id);
//                bundle.putString("type", "1");
//                intent.putExtras(bundle);
//                context.startActivity(intent);
                break;
            case 4://4同意好友申请
                break;
            case 5://5不同意好友申请
                break;
            case 6://6删除好友
                break;
            case 7://7订阅app即将到期
                intent = new Intent(context,BalanceActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                break;
        }
    }
}
